<?php
	header('content-type:text/html;charset=utf-8');	
	ini_set('date.timezone','Asia/Shanghai');
	
	require_once $_SITE_PATH_ . "m/m_information.php";
	require_once $_SITE_PATH_ . "m/m_agreement.php";
	require_once $_SITE_PATH_ . "m/m_tasklist.php";
	require_once $_SITE_PATH_."m/m_taskerror.php";
	require_once $_SITE_PATH_.'backend/myapi/UpFile_ApiClient.class.php';
		
			$apiid=isset($_POST['apiid'])?$_POST['apiid']:'';
			$apiflag=isset($_POST['apiflag'])?$_POST['apiflag']:'';
			
			$m_agreement=new m_agreement();
			$m_information=new m_information();
			$m_tasklists=new m_tasklist();
			$m_taskerror=new m_taskerror();
			$error_id='';
			if($apiflag==1)
				{
					/*
					$error_id=$apiid;
					$m_tasklist->_ssql='select `id`,`product_id` from tasklist where excuflag != "1" and ts_flag != "1" and `ts_type`="1" and `id` = '.$error_id.' order by `id` asc limit 0,1 ';
					$error=$m_tasklist->getArray($m_tasklist->query());
					$apiid=$error[0]['product_id']->id;
					*/
					
					$m_information->_ssql='select `id`,`code`,process_code,publish,`name`,isbn,bigclass_id,bigclass_name,smallclass_id,smallclass_name,`language`,`version`,price,num,`type`,r_date,editor,saler_sn,factory,author,print_page,publish_time,agreement_id,beizhu,is_upfile,catalog,p_cost,flow,is_volumes,books_nums,face_pic,plat_pic,copr_pic,code_pic,face_thumb,plat_thumb,copr_thumb,code_thumb,goods_file,create_user,create_date,originFile,processFile,finalFile FROM information where `flow`>=9 and ts_flag!="1" and `code` is not null and agreement_id is not null and `code`!="" and agreement_id!="" and `id` in ('.$apiid.') order by `id` asc ';
				}
				elseif($apiflag==2)
					{
						$m_information->_ssql='select `id`,`code`,process_code,publish,`name`,isbn,bigclass_id,bigclass_name,smallclass_id,smallclass_name,`language`,`version`,price,num,`type`,r_date,editor,saler_sn,factory,author,print_page,publish_time,agreement_id,beizhu,is_upfile,catalog,p_cost,flow,is_volumes,books_nums,face_pic,plat_pic,copr_pic,code_pic,face_thumb,plat_thumb,copr_thumb,code_thumb,goods_file,create_user,create_date,originFile,processFile,finalFile FROM information where `flow`>=9 and ts_flag!="1" and `code` is not null and agreement_id is not null and `code`!="" and agreement_id!="" and `id` in ('.$apiid.')  order by `id` asc ';
					}
					elseif($apiflag==3)
						{
							$m_information->_ssql='select `id`,`code`,process_code,publish,`name`,isbn,bigclass_id,bigclass_name,smallclass_id,smallclass_name,`language`,`version`,price,num,`type`,r_date,editor,saler_sn,factory,author,print_page,publish_time,agreement_id,beizhu,is_upfile,catalog,p_cost,flow,is_volumes,books_nums,face_pic,plat_pic,copr_pic,code_pic,face_thumb,plat_thumb,copr_thumb,code_thumb,goods_file,create_user,create_date,originFile,processFile,finalFile FROM information where `flow`>=9 and ts_flag!="1" and `code` is not null and agreement_id is not null and `code`!="" and agreement_id!="" order by `id` asc limit 0,3 ';
						}
						else
							{
								exit('Error: Invalid Access!');
							}
						
			//$m_information->_ssql='select `id`,`code`,process_code,publish,`name`,isbn,bigclass_id,bigclass_name,smallclass_id,smallclass_name,`language`,`version`,price,num,`type`,r_date,editor,saler_sn,factory,author,print_page,publish_time,agreement_id,beizhu,is_upfile,catalog,p_cost,flow,is_volumes,books_nums,face_pic,plat_pic,copr_pic,code_pic,face_thumb,plat_thumb,copr_thumb,code_thumb,goods_file,create_user,create_date,originFile,processFile,finalFile FROM information where `flow`>=9 and ts_flag!="1" and `code` is not null and agreement_id is not null and `code`!="" and agreement_id!="" order by `id` asc limit 0,3 ';
			
			
			
			
			$upfile_array=$m_information->getArray($m_information->query());
			
			 var_dump($upfile_array[0]);
			exit; 
			
			$count=0;
			if($apiflag==1 || $apiflag==2)
				{
					$count=$m_information->getCount(' `flow`>=9 and ts_flag!="1" and `code` is not null and agreement_id is not null and `code`!="" and agreement_id!="" and `id` in ('.$apiid.') ');
				}
				elseif($apiflag==3)
					{
						$count=$m_information->getCount(' `flow`>=9 and ts_flag!="1" and `code` is not null and agreement_id is not null and `code`!="" and agreement_id!="" ');
					}
			if(!$count)
				{
					//fopen();'没有数据要更新的！'
					exit;
				}
			//$count=count($upfile_array);
			echo $count;
			exit;
			$update_id='';
			$id='';
			$count=count($upfile_array);
			for($i=0;$i<$count;$i++)
				{
					$id.=','.$upfile_array[$i]->id;
					$update_id[$i]['id']=$upfile_array[$i]->id;
					unset($upfile_array[$i]->id);
				}
			$ts_id='';
			if(!empty($id))
				{
					$id=trim($id,',');
					$cur_count=$m_tasklist->getCount(' ts_type="0" and product_id='.$id.' and excuflag="0" and ts_flag="0" ');
					if($cur_count<=0 && ($apiflag == 2 || $apiflag == 3))
						{
							$m_tasklists->_ssql='insert into tasklist(ts_type,product_id,excuflag,ts_flag) values("0","'.$id.'","0","0")';
							$m_tasklists->query();
							$m_tasklists->_ssql='select last_insert_id() as lastid';
							$lastid=$m_tasklists->getArray($m_tasklists->query());
							$ts_id=$lastid[0]->lastid;
						}
					
					
				} 
				
			$post=isset($_POST['DATA'])?$_POST['DATA']:'';	
			
			$auth='tjiePjfjvnyGUuyMFEXVPmsewokCauuZ';
			$json_arr=array('data'=>$upfile_array,'count'=>$count);
			$json_arr=json_encode($json_arr);
			$time=time();
			$data=array('flag'=>'coprsys','method'=>'copr.add','type'=>'json','timestamp'=>$time,'charset'=>'utf-8','ver'=>1,'copr_data'=>$json_arr);
			$url='http://localhost/jieshou.php';
			$api=new UpFile_ApiClient();
			$result=$api->DoRequest($url,$auth,$data);
			if(get_magic_quotes_gpc())
				{
					$result=stripslashes($result);
				}
			$result=json_decode(trim(urldecode($result),chr(239).chr(187).chr(191)),true);
			
			if(strtolower($result['info'])=='success')
				{
					for($i=0;$i<$count;$i++)
						{
							$m_information->_ssql='update information set ts_flag="1" where `id`='.$update_id[$i]['id'];
							$m_information->query();
						}
					
					$m_tasklists->_ssql='update tasklist set excuflag="1",ts_flag="1" where `id`='.$ts_id;
					$m_tasklists->query();
					if($apiflag==1)
						{
							$m_tasklists->_ssql='update tasklist set excuflag="1",ts_flag="1" where `id`='.$error_id;
							$m_tasklists->query();
						}
					
					$m_tasklists->_ssql='select `id` from tasklist where ts_flag="0"';
					$array=$m_tasklists->getArray($m_tasklists->query());
					
					if(!empty($array))
						{
							//$up_id=implode(',',$array);
							$upcount=count($array);
							$up_id='';
							for($i=0;$i<$upcount;$i++)
								{
									$up_id.=$array[$i]->id.',';
								}
							$up_id=rtrim($up_id,',');
							$m_tasklists->_ssql='update tasklist set excuflag="1",ts_flag="1" where `id` in ('.$up_id.')';
							$m_tasklists->query();
						}
					//echo $result['info'];
					
					
					
					
					exit;
				}
				elseif(strtolower($result['info'])=='failed')
					{
						/* 
						if($apiflag!=1)
							{
								$m_tasklists->_ssql='update tasklist set excuflag="1",ts_flag="0" where `id`='.$ts_id;
								$m_tasklists->query();
							} 
						*/
						$m_tasklists->_ssql='update tasklist set excuflag="1",ts_flag="0" where `id`='.$ts_id;
						$m_tasklists->query();
						echo $result['info'];
						exit;
					}
					else
						{
							//echo 'FAILED';
							exit;
						}	

		
	

?>